package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Goods struct {
	Name string
	Price float64
}
type BaseGoods interface {
	GetName() string
	GetPrice() float64
}

func (this *Goods)GetName() string {
	return this.Name
}

func (this *Goods) GetPrice() float64 {
	return this.Price
}

type Computer struct {
	Goods
	Function string
}

type Car struct {
	Goods
	Skill string
}

func main() {
	goods := make([]BaseGoods, 0)
	goods=append(goods, &Computer{Goods{"苹果", 18000}, "上网"})
	goods=append(goods, &Car{Goods{"劳斯莱斯", 180000000}, "装"})

	jsonBytes, _ := json.Marshal(goods)
	fmt.Println(string(jsonBytes))


	dstFile,_ := os.OpenFile("./t.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND,0666)
	defer dstFile.Close()
	encoder := json.NewEncoder(dstFile)
	err := encoder.Encode(goods)
	if err!=nil{
		fmt.Println(err)
	}
	for _, good := range goods {
		  //写入文件
		encoder.Encode(good)
	}
}
